package com.zb.frame.modules.monitor.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zb.frame.base.model.R;
import com.zb.frame.modules.monitor.entity.Log;
import com.zb.frame.modules.monitor.enums.LogType;
import com.zb.frame.modules.monitor.service.LogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bangZZ
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/monitor/log")
public class LogController {

    @Autowired
    LogService logService;

    /**
     * (因为restful风格，所以做成get请求，吐槽一下)
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R getLogList(@RequestParam Map<String, Object> params){
        Map<String, Object> query = new HashMap<>();
        JSONObject object = JSON.parseObject((String) params.get("query"));
        Integer page = object.get("page") == null ? 1 : (Integer) object.get("page");
        Integer limit = object.get("limit") == null ? 20 : (Integer) object.get("limit");
        String operatorName = (String) object.get("opUser");
        String logType = String.valueOf(object.get("type"));
        String logModule = (String) object.get("module");
        String startTime = (String) object.get("startTime");
        String endTime = (String) object.get("endTime");
        String okFlag = String.valueOf(object.get("status"));
        String batchFlag = (String) object.get("batchFlag");
        String returnFlag = (String) object.get("returnFlag");
        if (StringUtils.isNotEmpty(operatorName)) {
            query.put("operatorName", operatorName);
        }
        if (StringUtils.isNotEmpty(logType)) {
            query.put("logType", Integer.parseInt(logType));
        }
        if (StringUtils.isNotEmpty(logModule)) {
            query.put("logModule", logModule);
        }
        if (StringUtils.isNotEmpty(startTime)) {
            query.put("startTime", startTime + " 00:00:00");
        }
        if (StringUtils.isNotEmpty(endTime)) {
            query.put("endTime", endTime + " 23:59:59");
        }
        if (StringUtils.isNotEmpty(okFlag)) {
            query.put("okFlag", Integer.parseInt(okFlag));
        }
        if (StringUtils.isNotEmpty(batchFlag)) {
            query.put("batchFlag", Integer.parseInt(batchFlag));
        }
        if (StringUtils.isNotEmpty(returnFlag)) {
            query.put("returnFlag", Integer.parseInt(returnFlag));
        }
        List<Log> logList = logService.getLogList(page, limit, query);
        Long count = logService.getLogListCount(query);
        Map<String, Object> ret = new HashMap<>();
        ret.put("dataList", logList);
        ret.put("total", count);
        return R.ok(ret);
    }


    public Log newLogObj(LogType type, String oldValue, String newValue, String relId, Long operator, boolean batch){
        Log log = new Log();
        log.setLogType(type.getType());
        log.setOldValue(oldValue);
        log.setNewValue(newValue);
        log.setRelId(relId);
        log.setOperator(operator);
        log.setReturnFlag(0);
        log.setBatchFlag(batch ? 1 : 0);
        log.setOkFlag(1);
        log.setFailMsg("");
        return log;
    }

    public Log newLogObj(LogType type, String oldValue, String newValue, String relId, Long operator, boolean batch, boolean success, String error){
       Log log = newLogObj(type, oldValue, newValue, relId, operator, batch);
       log.setOkFlag(success ? 1: 0);
        log.setFailMsg(error);
       return log;
    }

}
